package com.hck.app.service;

import com.hck.api.model.DoctorAuthenticateRequestDTO;
import com.hck.api.model.DoctorLoginRequestDTO;
import com.hck.api.model.DoctorLoginResponseDTO;
import com.hck.api.model.DoctorUpdatePasswordRequestDTO;
import com.hck.api.model.DoctorUpdateProfileRequestDTO;
import com.hck.domain.doctor.model.entity.DoctorEntity;
import com.hck.domain.doctor.model.valobj.LoginRequest;
import com.hck.domain.doctor.model.valobj.LoginResult;
import com.hck.domain.doctor.service.DoctorDomainService;
import com.hck.domain.doctor.service.IDoctorService;
import com.hck.infrastructure.redis.IRedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * DoctorService class description here.
 *
 * @author 黄朝看
 * @date 2025/7/2
 */
@Service
public class DoctorService implements IDoctorService {
    @Resource
    private DoctorDomainService domainService;
    
    @Resource
    private IRedisService redisService;

    @Override
    public DoctorLoginResponseDTO login(DoctorLoginRequestDTO requestDTO) {
        LoginRequest loginRequest = new LoginRequest();

        loginRequest.setLoginName(requestDTO.getLoginName());
        loginRequest.setPassword(requestDTO.getPassword());

        LoginResult loginResult =  domainService.login(loginRequest);

        DoctorLoginResponseDTO doctorLoginResponseDTO = new DoctorLoginResponseDTO();
        doctorLoginResponseDTO.setSuccess(loginResult.isSuccess());
        doctorLoginResponseDTO.setErrorMessage(loginResult.getErrorMessage());

        if(loginResult.isSuccess()){
            doctorLoginResponseDTO.setLoginName(loginResult.getLoginName());
            doctorLoginResponseDTO.setDoctorId(loginResult.getDoctorId());
            doctorLoginResponseDTO.setRealName(loginResult.getRealName());
            String token = "doctor_token_" + loginResult.getDoctorId() + "_" + System.currentTimeMillis();
            doctorLoginResponseDTO.setToken(token);
            
            // 缓存医生Token和登录信息
            redisService.setValue("doctor:token:" + loginResult.getDoctorId(), token);
            redisService.setValue("doctor:login:" + loginResult.getDoctorId(), token);
        }
        return doctorLoginResponseDTO;
    }

    @Override
    public DoctorEntity getDoctorInfo(Long doctorId) {
        // 1. 先从缓存获取医生信息
        DoctorEntity doctor = redisService.getValue("doctor:info:" + doctorId);
        
        if (doctor == null) {
            // 2. 缓存中没有，从数据库获取
            doctor = domainService.getDoctorInfo(doctorId);
            
            if (doctor != null) {
                // 3. 将医生信息缓存到Redis
                redisService.setValue("doctor:info:" + doctor.getDoctorId(), doctor);
            }
        }
        
        return doctor;
    }

    @Override
    public List<DoctorEntity> getAllDoctors() {
        // 1. 先从缓存获取医生列表
        List<DoctorEntity> doctors = redisService.getValue("doctor:list:all");
        
        if (doctors == null) {
            // 2. 缓存中没有，从数据库获取
            doctors = domainService.getAllDoctors();
            
            if (doctors != null && !doctors.isEmpty()) {
                // 3. 将医生列表缓存到Redis
                redisService.setValue("doctor:list:all", doctors);
            }
        }
        
        return doctors;
    }

    @Override
    public boolean updatePassword(DoctorUpdatePasswordRequestDTO requestDTO) {
        System.out.println("更新医生密码: doctorId=" + requestDTO.getDoctorId());
        
        boolean result = domainService.updatePassword(requestDTO);
        
        if (result) {
            // 密码修改成功，清除相关缓存
            redisService.remove("doctor:token:" + requestDTO.getDoctorId());
            redisService.remove("doctor:login:" + requestDTO.getDoctorId());
        }
        
        return result;
    }

    @Override
    public boolean updateProfile(DoctorUpdateProfileRequestDTO requestDTO) {
        System.out.println("更新医生信息: doctorId=" + requestDTO.getDoctorId() + ", avatar=" + requestDTO.getAvatar());
        
        boolean result = domainService.updateProfile(requestDTO);
        
        if (result) {
            // 信息更新成功，清除相关缓存
            redisService.remove("doctor:info:" + requestDTO.getDoctorId());
            redisService.remove("doctor:list:all");
        }
        
        return result;
    }

    @Override
    public boolean authentic(DoctorAuthenticateRequestDTO requestDTO) {
        return domainService.authentic(requestDTO);
    }
}
